package com.example.smartcity_40.Outpatient;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.example.smartcity_40.Outpatient.apter.RegularregistrationApter;
import com.example.smartcity_40.Outpatient.bean.DepartmentBean;
import com.example.smartcity_40.Outpatient.bean.OutpatienttypeBean;
import com.example.smartcity_40.Outpatient.bean.RegularregistrationBean;
import com.example.smartcity_40.R;
import com.example.smartcity_40.tool.Tool;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import kotlin.Unit;
import kotlin.jvm.functions.Function1;

public class DepartmentActivity extends AppCompatActivity {

    private DepartmentActivity context;
    private TabLayout mytab;
    private Tool tool;
    private RecyclerView departmentlist;
    private RegularregistrationApter regularregistrationApter;
    private ImageView add;
    private int ADDVISITORID = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_department);
        context = this;
        initview();
        departmenymethod();
    }

    private void departmenymethod() {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("专家挂号");
        strings.add("普通挂号");

        for (String string : strings) {
            TabLayout.Tab tab = mytab.newTab();
            tab.setText(string);
            mytab.addTab(tab);
        }

        mytab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                String type = tab.getText().toString();
                if("普通挂号".equals(type)){
                    sendregularregistration();
                    departmentlist.setVisibility(View.VISIBLE);
                }else {
                    departmentlist.setVisibility(View.GONE);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void sendregularregistration() {
        tool.send("/prod-api/api/hospital/reservation/list",
                Tool.GET,
                null,
                true,
                DepartmentBean.class,
                new Function1<DepartmentBean, Unit>() {
                    @Override
                    public Unit invoke(DepartmentBean departmentBean) {
                        departmentlist.setLayoutManager(new LinearLayoutManager(context));
                        regularregistrationApter.setDate(departmentBean.getRows());
                        departmentlist.setAdapter(regularregistrationApter);
                        return null;
                    }
                });
    }


    private void initview() {
        ImageView back = findViewById(R.id.back);
        departmentlist = findViewById(R.id.departmentlist);
        add = findViewById(R.id.add);
        mytab = findViewById(R.id.mytab);
        tool = new Tool(context);
        regularregistrationApter = new RegularregistrationApter(context);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.finish();
            }
        });

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,AddReservationActivity.class);
                context.startActivityForResult(intent,ADDVISITORID);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
       //Log.e("TAG","requestCode"+requestCode);   1
        //Log.e("TAG","resultCode"+resultCode);    -1
        if( requestCode==ADDVISITORID && resultCode ==RESULT_OK){
            sendregularregistration();
        }
    }
}